/**
 * Created by PengWei on 2018/7/24.
 */
namespace mengdao {
    export function instantiateUnmapped<T>(injector: mengdao.Injector, type: mengdao.IClass<T>): T {
        injector.bind(type).toConstSelf();
        let instance: T = injector.getFromBind<T>(type);
        injector.unbind(type);
        return instance;
    }

    export function getUrlParams(): any {
        let query_string = {};
        let query = window.location.search.substring(1);
        let vars = query.split("&");
        for (let i = 0; i < vars.length; i++) {
            let pair = vars[i].split("=");
            // If first entry with this name
            if (typeof query_string[pair[0]] === "undefined") {
                query_string[pair[0]] = decodeURIComponent(pair[1]);
                // If second entry with this name
            }
            else if (typeof query_string[pair[0]] === "string") {
                let arr = [
                    query_string[pair[0]],
                    decodeURIComponent(pair[1])
                ];
                query_string[pair[0]] = arr;
                // If third or later entry with this name
            }
            else {
                query_string[pair[0]].push(decodeURIComponent(pair[1]));
            }
        }
        return query_string;
    }

    export function isClassExtends(targetClass: any, extendsClass: any): boolean {
        let target = targetClass.prototype.__proto__;
        let is = target.constructor == extendsClass;
        while (!is && target.__proto__) {
            target = target.__proto__;
            is = target.constructor == extendsClass;
        }
        return is;
    }
}